<template>
  <div class="container">
    <el-dialog
      width="400px"
      :visible="showCheck"
      title="题目审核"
      @close="btnCancel"
    >
      <template>
        <el-radio-group v-model="currSub.chkState">
          <el-radio :label="1">通过</el-radio>
          <el-radio :label="2">拒绝</el-radio>
        </el-radio-group>
        <el-form ref="chkRemarksForm" :rules="rules" :model="currSub">
          <el-form-item prop="chkRemarks">
            <el-input
              v-model="currSub.chkRemarks"
              style="margin: 20px 0"
              type="textarea"
              placeholder="请输入审核意见"
            ></el-input>
          </el-form-item>
        </el-form>
        <template slot="footer">
          <el-button @click="btnCancel">取消</el-button>
          <el-button type="primary" @click="btnOK">确认</el-button>
        </template>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { choiceCheck } from '@/api/hmmm/questions'
export default {
  name: 'Check',
  props: {
    currSub: {
      required: true,
      default: () => ({})
    },
    showCheck: {
      required: true,
      default: false
    }
  },
  data () {
    // 自定义审核意见校验规则
    const validatePass = (rule, value, callback) => {
      if (!value) {
        this.$message.warning('请输入审核意见')
        return null
      } else {
        callback()
      }
    }
    return {
      chkState: 0, // 审核状态
      rules: {
        chkRemarks: [{ validator: validatePass, trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 点击关闭按钮事件句柄
    btnCancel () {
      this.$emit('update:showCheck', false)
    },
    async btnOK () {
      const { id, chkState, chkRemarks } = this.currSub
      console.log(id, chkState, chkRemarks)
      try {
        await this.$refs.chkRemarksForm.validate()
        await choiceCheck({ id, chkState, chkRemarks })
        this.$message.success('操作成功')
        this.btnCancel()
        this.$emit('updateSubList')
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang='scss'></style>
